package com.h2.geo.portal.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.GeoXmlLoadCallback;
import com.google.gwt.maps.client.overlay.GeoXmlOverlay;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GeoPortal implements EntryPoint {
   
   interface Binder extends UiBinder<VerticalPanel, GeoPortal> {}
   private static Binder UI_BINDER = GWT.create(Binder.class);
   
   ////
   
   private static String KML_DEMO_URI = 
      "http://core-portal.googlecode.com/svn/trunk/com.h2.geo.portal/war/data/all_cores.kml";
   
//   private static String KML_DEMO_URI =
//      "http://gmaps-samples.googlecode.com/svn/trunk/ggeoxml/cta.kml";
   
   private GeoXmlOverlay _geoXml;
   
   private MapWidget _map;
   
//   @UiField
//   public Button _addButton;
   
   /////
   /////

   /**
    * This is the entry point method.
    */
   public void onModuleLoad() {
      
      VerticalPanel widget = UI_BINDER.createAndBindUi(this);
      
      widget.add(getMap());
      
      RootPanel.get().add(widget);
      
      loadOverlay();
   }
   
   public void loadOverlay() {
      GeoXmlOverlay.load(KML_DEMO_URI, new GeoXmlLoadCallback() {

         @Override
         public void onFailure(String url, Throwable exp) {
           StringBuffer message = new StringBuffer("KML File " + url
               + " failed to load");
           if (exp != null) {
             message.append(exp.toString());
           }
           Window.alert(message.toString());
         }

         @Override
         public void onSuccess(String url, GeoXmlOverlay overlay) {
           if (_geoXml != null) {
             return;
           }
           _geoXml = overlay;
//           removeAddButton.setEnabled(true);
           GWT.log("KML File " + url + " loaded successfully", null);
           GWT.log("Default Center=" + _geoXml.getDefaultCenter(), null);
           GWT.log("Default Span=" + _geoXml.getDefaultSpan(), null);
           GWT.log("Default Bounds=" + _geoXml.getDefaultBounds(), null);
           GWT.log("Supports hide=" + _geoXml.supportsHide(), null);
           toggleOverlay();
         }
       });
   }
   
//   @UiHandler("_photoButton")
//   public void handlePhotoButtonClick(ClickEvent event) {
//      
//   }
   
   private void toggleOverlay() {
      if (_geoXml == null) {
        return;
      }
      getMap().addOverlay(_geoXml);
      
    }
   
   public MapWidget getMap() {
      if(_map == null) {
         _map = new MapWidget();
         _map.setSize("800px", "500px");
         _map.setUIToDefault();
         // -80.992383,35.169223
         _map.setCenter(LatLng.newInstance(35.18812, -80.96426), 13);
      }
      return _map;
   }
}
